﻿using System;
using System.ComponentModel;

namespace Silverlight
{
    /// <summary>
    /// Base class for generated DTO
    /// </summary>
    public class BindableObject : INotifyPropertyChanged
    {
        #region Data Fields
        /// <summary>
        /// Holds reference to DataRow instance
        /// </summary>
        private DataRow _dataRow;
        #endregion

        #region Events
        /// <summary>
        /// INotifyPropertyChanged interface implementation
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion

        #region Constructors
        /// <summary>
        /// Used for DTO inheritance
        /// </summary>
        /// <param name="dataRow">DataRow instance</param>
        public BindableObject(DataRow dataRow)
        {
            // Validate argument
            if (dataRow == null)
            {
                throw new ArgumentNullException("dataRow");
            }

            this._dataRow = dataRow;
            this._dataRow.PropertyChanged += new PropertyChangedEventHandler(DataRow_PropertyChanged);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets DataRow instance
        /// </summary>
        public DataRow DataRow
        {
            get
            {
                return this._dataRow;
            }
        }
        #endregion

        #region Methods
        /// <summary>
        /// Gets field error
        /// </summary>
        /// <param name="columnName">Column name</param>
        /// <returns>Field error</returns>
        public string GetError(string columnName)
        {
            return this._dataRow.ItemArray[columnName].FieldError;
        }

        /// <summary>
        /// Gets "HasError" flag value
        /// </summary>
        /// <param name="columnName">Column name</param>
        /// <returns>True if column has error</returns>
        public bool GetHasError(string columnName)
        {
            return this._dataRow.ItemArray[columnName].HasError;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="columnName"></param>
        /// <returns></returns>
        public bool GetHasNoErrors(string columnName)
        {
            return this._dataRow.HasNoErrors;
        }

        /// <summary>
        /// Gets value from specified field
        /// </summary>
        /// <param name="columnName">Column name</param>
        /// <returns>Field value</returns>
        public string GetValue(string columnName)
        {
            return this._dataRow[columnName];
        }

        /// <summary>
        /// Bubbles PropertyChanged event
        /// </summary>
        /// <param name="sender">Event source</param>
        /// <param name="e">Event arguments</param>
        private void OnPropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(sender, e);
            }
        }

        /// <summary>
        /// Sets value into specified field
        /// </summary>
        /// <param name="columnName">Column name</param>
        /// <param name="value">Field value</param>
        public void SetValue(string columnName, string value)
        {
            this._dataRow[columnName] = value;
        }
        #endregion

        #region Event Handlers
        /// <summary>
        /// Handles DataRow.PropertyChanged event
        /// </summary>
        /// <param name="sender">Event source</param>
        /// <param name="e">Event arguments</param>
        void DataRow_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            this.OnPropertyChanged(sender, e);
        }
        #endregion
    }
}
